﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SettlementDetailForSalesOrderShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (Request.QueryString["SalesOrderID"] != null)
                {
                    if (mk.IsNumber(Request.QueryString["SalesOrderID"].ToString()))
                    {
                        //用一个隐藏Label保存传递过来的ID
                        this.lblSalesOrderID.Text = Request.QueryString["SalesOrderID"].ToString();
                    }
                    else
                    {
                        mk.Alert("参数错误！");
                    }
                }
                else
                {
                    mk.Alert("参数错误！");
                }
                //显示订单概况
                this.ShowSalesOrderInfo();
                this.BindSettlementDetailForSalesOrder();
                this.BindSettlementDetailForPurchaseOrder();
            }
        }
        private void ShowSalesOrderInfo()
        {
           var bll =mk.l.Sales_SalesOrderHeader.ToList();
                   Sales_SalesOrderHeader m = bll.Where(h=>h.SalesOrderID==int.Parse(this.lblSalesOrderID.Text)).FirstOrDefault();
            if (m != null)
            {
                //设置经办人
               Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                 this.lblRootDepartmentID.Text = bllDept.Where(j=>j.ClassID==(int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                if (int.Parse(this.lblRootDepartmentID.Text) == m.RootDepartmentID)
                {
                    this.lblBrokerage.Text = m.Brokerage.ToString();
                    this.lblContractAmount.Text = m.ContractAmount.ToString();
                    this.lblPrepaidPayment.Text = m.PrepaidPayment.ToString();
                    this.lblResidualPayment.Text = m.ResidualPayment.ToString();
                    this.lblSubTotal.Text = m.SubTotal.ToString();
                    this.lblTotalDue.Text = m.TotalDue.ToString();
                }
                else
                {
                   mk.Alert("只能查看本店的订单！");
                }
            }
            else
            {
               mk.Alert("参数错误！");
            }
        }

        /// <summary>
        /// Binds the settlement detail for sales order.
        /// </summary>
        private void BindSettlementDetailForSalesOrder()
        {
           // UART.BLL.Sales.SettlementDetail bll = new UART.BLL.Sales.SettlementDetail();
       var ds = mk.l.Sales_SettlementDetail.Where(y=>y.SalesOrderID.ToString()==this.lblSalesOrderID.Text).ToList();
            this.myGridViewInput.DataSource = ds;
            this.myGridViewInput.DataBind();
        }

        /// <summary>
        /// Binds the settlement detail for purchase order.
        /// </summary>
        private void BindSettlementDetailForPurchaseOrder()
        {
           // UART.BLL.Sales.SettlementDetail bll = new UART.BLL.Sales.SettlementDetail();
            var ds = from p in mk.l.Sales_SettlementDetail where (from d in mk.l.Sales_PurchaseOrderHeader where d.SalesOrderID.ToString() == this.lblSalesOrderID.Text select d.PurchaseOrderID).Contains(int.Parse(p.PurchaseOrderID.ToString())) select p;
        //    DataSet ds = bll.GetList(" PurchaseOrderID in (select PurchaseOrderID from Sales_PurchaseOrderHeader where SalesOrderID=" + this.lblSalesOrderID.Text + ")");
            this.myGridViewOutput.DataSource = ds;
            this.myGridViewOutput.DataBind();
        }

        /// <summary>
        /// Handles the RowDataBound event of the myGridViewInput control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void myGridViewInput_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //显示账户名称
                Label lblAccountName = (Label)(e.Row.FindControl("lblAccountName"));
                var bllAccount =mk.l.Sales_Account.ToList();
           Sales_Account mAccount = bllAccount.Where(h=>h.AccountID==int.Parse(this.myGridViewInput.DataKeys[e.Row.RowIndex].Values["AccountID"].ToString())).FirstOrDefault();
                if (mAccount != null)
                {
                    lblAccountName.Text = mAccount.Name;
                }

                //显示收支类别
                Label lblTypeName = (Label)(e.Row.FindControl("lblTypeName"));
               var bllSettlementType =mk.l.Sales_SettlementType.ToList();
              Sales_SettlementType mSettlementType = bllSettlementType.Where(h=>h.TypeID==int.Parse(this.myGridViewInput.DataKeys[e.Row.RowIndex].Values["TypeID"].ToString())).FirstOrDefault();
                if (mSettlementType != null)
                {
                    lblTypeName.Text = mSettlementType.Name;
                }

                //显示经办人姓名
                Label lblManager = (Label)(e.Row.FindControl("lblManager"));
               var bllUser = mk.l.Accounts_Users.ToList();
               Accounts_Users mUser = bllUser.Where(h=>h.UserName==this.myGridViewInput.DataKeys[e.Row.RowIndex].Values["Manager"].ToString()).FirstOrDefault();
                lblManager.Text = mUser.TrueName;


                //显示状态
                Label lblFlag = (Label)(e.Row.FindControl("lblFlag"));
                string strFlag = this.myGridViewInput.DataKeys[e.Row.RowIndex].Values["Flag"].ToString();
                switch (strFlag)
                {
                    case "1":
                        lblFlag.Text = "支出";
                        break;
                    case "2":
                        lblFlag.Text = "收入";
                        break;
                    default:
                        lblFlag.Text = "未知";
                        break;
                }

                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }
        }

        /// <summary>
        /// Handles the RowDataBound event of the myGridViewOutput control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void myGridViewOutput_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //显示账户名称
                Label lblAccountName = (Label)(e.Row.FindControl("lblAccountName"));
         var  bllAccount = mk.l.Sales_Account.ToList();
               Sales_Account mAccount = bllAccount.Where(h=>h.AccountID==int.Parse(this.myGridViewOutput.DataKeys[e.Row.RowIndex].Values["AccountID"].ToString())).FirstOrDefault();
                if (mAccount != null)
                {
                    lblAccountName.Text = mAccount.Name;
                }

                //显示收支类别
                Label lblTypeName = (Label)(e.Row.FindControl("lblTypeName"));
             var bllSettlementType = mk.l.Sales_SettlementType.ToList();
              Sales_SettlementType mSettlementType = bllSettlementType.Where(h=>h.TypeID==int.Parse(this.myGridViewOutput.DataKeys[e.Row.RowIndex].Values["TypeID"].ToString())).FirstOrDefault();
                if (mSettlementType != null)
                {
                    lblTypeName.Text = mSettlementType.Name;
                }

                //显示经办人姓名
                Label lblManager = (Label)(e.Row.FindControl("lblManager"));
               var bllUser = mk.l.Accounts_Users.ToList();
             Accounts_Users mUser = bllUser.Where(h=>h.UserName==this.myGridViewOutput.DataKeys[e.Row.RowIndex].Values["Manager"].ToString()).FirstOrDefault();
                lblManager.Text = mUser.TrueName;


                //显示状态
                Label lblFlag = (Label)(e.Row.FindControl("lblFlag"));
                string strFlag = this.myGridViewOutput.DataKeys[e.Row.RowIndex].Values["Flag"].ToString();
                switch (strFlag)
                {
                    case "1":
                        lblFlag.Text = "支出";
                        break;
                    case "2":
                        lblFlag.Text = "收入";
                        break;
                    default:
                        lblFlag.Text = "未知";
                        break;
                }

                //显示订货单编号
              var bllHeader = mk.l.Sales_PurchaseOrderHeader.ToList();
      Sales_PurchaseOrderHeader mHeader = bllHeader.Where(h=>h.PurchaseOrderID==int.Parse(this.myGridViewOutput.DataKeys[e.Row.RowIndex].Values["PurchaseOrderID"].ToString())).FirstOrDefault();
                Label lblPurchaseOrderNumber = (Label)e.Row.FindControl("lblPurchaseOrderNumber");
                lblPurchaseOrderNumber.Text = mHeader.OrderNumber;

                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }
        }
    }
}
